package cn.lxh.test;

import cn.lxh.basic.utils.HttpUtil;
import cn.lxh.basic.utils.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WxTemplateDemo
 * @Description WxTemplateDemo
 * @Author luoxiaohu
 * @Date 2023/7/18 16:05
 * @Vsersion 1.0
 **/
@Slf4j
public class WxTemplateDemo {
    //测试推送消息
    public static void main(String[] args) {
        JSONObject param = new JSONObject();
        JSONObject dataParam = new JSONObject();
        param.put("touser", "oaFOT6rEUiM7pP5O5uqFqL-FnwDw");
        param.put("template_id", "E5Tl-WGxsBYETE8S98s784bT8AW4r9JoMt15m4uxa_Y");
        param.put("url", "");
        Map<String,String> note= new HashMap<>();
        Map<String,String> content= new HashMap<>();
        Map<String,String> picture= new HashMap<>();

        note.put("value","标题啊！！");
        note.put("color","#173177");

        content.put("value","内容啊啊啊！！");
        content.put("color","#173177");

        picture.put("value","图片");
        picture.put("color","#173177");

        dataParam.put("note", note);
        dataParam.put("content", content);
        dataParam.put("picture",picture);

        param.put("data", dataParam);
        log.info("微信消息入参{}",param);
        sendWechatSms(param,"wxb5cb3cec24f258ed","00d735eaa51bd723d52ff3e4ce3c072f");
    }

    public static void sendWechatSms( JSONObject param,String appid,String secret)   {

        if (StringUtils.isNotBlank(param.toString())) {
            log.info("微信消息推送开始");
            //获取access_token
//            String url = "70_UWsmn_byUQLY8NdCsA8QIdaNJYuBUzxmBwbHCLmADbi3EKPLHIHDh8WsKzyXjCoQ3_4D_ERdKSIerroJ4ZzXPJXPeHj3CARxfktYN5nfxDOpiTtlC-RsCrsUflYCXNiAHAWRH" + "?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
//            String info = HttpUtils.get(url);
//            JSONObject jsonObject = JSONObject.parseObject(info);
            if (true) {
//                String accessToken = jsonObject.get("access_token").toString();
                log.info("微信通知,消息推送参数{}", param);
                String accessToken = "70_UWsmn_byUQLY8NdCsA8QIdaNJYuBUzxmBwbHCLmADbi3EKPLHIHDh8WsKzyXjCoQ3_4D_ERdKSIerroJ4ZzXPJXPeHj3CARxfktYN5nfxDOpiTtlC-RsCrsUflYCXNiAHAWRH";
                try {

                    String sendResult = HttpUtils.postJson("https://api.weixin.qq.com/cgi-bin/message/template/send"+ "?access_token=" + accessToken, param.toJSONString());
                    log.info("消息结果{}", sendResult);

                    JSONObject resultJson = JSON.parseObject(sendResult);
                    if (Integer.parseInt(resultJson.get("errcode").toString()) == 0) {
                        log.info("消息推送成功");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            log.info("微信消息推送结束");

        }
    }

}
